package com.hb.B_面向对象.J_lambda和集合.B_集合的高阶函数API

fun main() {
    val student1 = Student("student1", 30, "男", 85)
    val student2 = Student("student2", 20, "男", 80)
    val student3 = Student("student3", 25, "女", 82)
    val student4 = Student("student4", 28, "女", 88)
    val student5 = Student("student5", 35, "男", 89)

    var studentList = listOf<Student>(student1, student2, student3, student4, student5)
    //filter 找出符合条件的学生并打印
    /**
     * filter底层调用的是filterTo，的意思是：传入一个集合，判断集合中数据是否符合 predict的断言
     * public inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C
     */
    studentList.filter {
        it.score>85 && it.sex=="男"
    }.forEach(::println)

    studentList.filterNotNull()

    //count:统计满足条件的个数
    var count=studentList.count {
        it.sex=="男"
    }
    println("count=$count")

}

data class Student(val name:String,val age:Int,val sex:String,val score:Int)